package com.water_work_form.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deepoove.poi.XWPFTemplate;
import com.water_work_form.entity.BasicRestriction;
import com.water_work_form.entity.RecordDisinfect;
import com.water_work_form.mapper.*;
import com.water_work_form.service.RecordDisinfectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 记录——消毒实验原始记录 服务实现类
 * </p>
 *
 * @author xuke
 * @since 2023-08-25
 */
@Service
public class RecordDisinfectServiceImpl extends ServiceImpl<RecordDisinfectMapper, RecordDisinfect> implements RecordDisinfectService {

    @Autowired
    RecordDisinfectMapper recordDisinfectMapper;

    @Autowired
    BasicFormMapper basicFormMapper;

    @Autowired
    BasicRestrictionMapper basicRestrictionMapper;

    @Autowired
    BasicDeviceMapper basicDeviceMapper;

    @Autowired
    BasicDrugMapper basicDrugMapper;

    @Value("${file.templateFilePath}")
    private String templateFilePath;

    @Value("${file.destFilePath}")
    private String destFilePath;

    /**
     * 记录——消毒实验原始记录 项目查询
     * @param current 分页 当前页
     * @param status 状态（status）
     * @param size 分页 每页数据量
     * @return 返回相应数据 按照状态（status）升序排序
     */
    @Override
    public IPage<RecordDisinfect> selectRecordDisinfect(String status, Integer current, Integer size){
        QueryWrapper<RecordDisinfect> queryWrapper = new QueryWrapper<>();
        if(!status.isEmpty()){
            queryWrapper.eq("status", status);
        }
        queryWrapper.orderByAsc("status");
        IPage<RecordDisinfect> page = new Page<>(current, size);
        return this.page(page, queryWrapper);
    }

    /**
     * 记录——消毒实验原始记录 基础表数据查询
     * @param name form表name
     * @param restrictionName29 restriction表name
     * @param restrictionName30 restriction表name
     * @param type device表name
     * @param drugName drug表name
     * @return 返回相应数据 表名为键名，不止一项查询项则键名与查询项数字编号一致
     */
    @Override
    public Map<String,Object> selectBasicDisinfect(String name, String restrictionName29, String restrictionName30, String type, String drugName) {
        Map<String,Object> map = new HashMap<>();
        map.put("form", basicFormMapper.getOneByName(name));
        QueryWrapper<BasicRestriction> queryWrapper = new QueryWrapper<>();
        map.put("restriction", basicRestrictionMapper.selectList(queryWrapper
                .eq("name",restrictionName29).or().eq("name",restrictionName30).select("name","check_content")));
        map.put("device", basicDeviceMapper.getOneByName(type));
        map.put("drug", basicDrugMapper.getOneByName(drugName));
        return map;
    }

    /**
     * 记录——消毒实验原始记录 数据表导出
     * @param recordDisinfect 需要导出的数据表
     * @throws IOException 异常
     */
    @Override
    public void downloadRecordDisinfect(RecordDisinfect recordDisinfect) throws IOException {
        Map<String, Object> data = new HashMap<String, Object>();
        JSONObject chlorineLiquidDosageDetail =  JSON.parseObject(recordDisinfect.getChlorineLiquidDosageDetail());
        JSONObject residualChlorineDetail =  JSON.parseObject(recordDisinfect.getResidualChlorineDetail());
        data.put("chlorineLiquidDosageDetail", chlorineLiquidDosageDetail);
        data.put("residualChlorineDetail", residualChlorineDetail);
        data.put("list", recordDisinfect);
        XWPFTemplate compile = XWPFTemplate.compile(templateFilePath + "RecordDisinfect.docx");
        compile.render(data);
        compile.writeToFile(destFilePath + recordDisinfect.getId() + "out_recordDisinfect.docx");
        compile.close();
    }


}
